﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;
using System.Collections;
using System.Data.SqlClient;
using QLTuyenXe.library;

namespace QLTuyenXe.GUI
{
    public partial class frmDanhSachKH : DevComponents.DotNetBar.Office2007RibbonForm
    {
        BKhachHang kh = new BKhachHang();
        XuLyDL xl = new XuLyDL();
        KHACHHANG khachHang = new KHACHHANG();
        ThongBao thongBao = new ThongBao();
        public static string ten = string.Empty;
        public frmDanhSachKH()
        {
            InitializeComponent();
        }

        private void frmDanhSachKH_Load(object sender, EventArgs e)
        {
            HT();
            CheckQuyen();
            btnTimKiem.Enabled = false;
            
        }
        public void HT()
        {
            ArrayList khachHang = kh.Select();
            DataTable tbKH = new DataTable();
           
            //tbKH.Columns.Add("STT", typeof(String));
            tbKH.Columns.Add("Mã KH", typeof(string));
            tbKH.Columns.Add("Tên Công ty(Đơn vị)", typeof(String));
            tbKH.Columns.Add("Đại diện", typeof(String));
            tbKH.Columns.Add("Chức vụ", typeof(String));
            tbKH.Columns.Add("Địa chỉ", typeof(String));
            tbKH.Columns.Add("Số DT", typeof(String));
            if (khachHang.Count > 0)
                foreach (KHACHHANG tx in khachHang)
                {
                    DataRow r = tbKH.NewRow();
                    r["Mã KH"] = tx.MaKH;
                    r["Tên Công ty(Đơn vị)"] = tx.DonviB;
                    r["Đại diện"] = tx.DaidienB;
                    r["Chức vụ"] = tx.ChucvuB;
                    r["Địa chỉ"] = tx.DiachiB;
                    r["Số DT"] = tx.DienthoaiB;
                    tbKH.Rows.Add(r);
                }
            dgvKhachHang.DataSource= tbKH;
            //dgvKhachHang.Columns[0].Visible = false;
            dgvKhachHang.RowHeadersVisible = false;
            dgvKhachHang.Columns[0].ReadOnly = true;           
            dgvKhachHang.Columns[0].Width = 100;
            dgvKhachHang.Columns[1].Width = 388;            
            dgvKhachHang.Columns[2].Width = 250;
            dgvKhachHang.Columns[3].Width = 120;            
            dgvKhachHang.Columns[4].Width = 340;
            dgvKhachHang.Columns[5].Width = 150;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {

        }

        private void dgvKhachHang_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKhachHang.CurrentRow.Cells[1].Value.ToString() != "" && dgvKhachHang.CurrentRow.Cells[2].Value.ToString() != "" && dgvKhachHang.CurrentRow.Cells[4].Value.ToString() != "" && dgvKhachHang.CurrentRow.Cells[4].Value.ToString() != "" && dgvKhachHang.CurrentRow.Cells[5].Value.ToString() != "")
            {
                if (xl.nhom(dgvKhachHang.CurrentRow.Cells[1].Value.ToString()) == true && xl.HoTen(dgvKhachHang.CurrentRow.Cells[2].Value.ToString()) == true && xl.nhom(dgvKhachHang.CurrentRow.Cells[4].Value.ToString()) == true)
                {
                    //DataTable tb = kh.Select_DK(dgvKhachHang.CurrentRow.Cells[1].Value.ToString(), dgvKhachHang.CurrentRow.Cells[2].Value.ToString(), dgvKhachHang.CurrentRow.Cells[4].Value.ToString(), dgvKhachHang.CurrentRow.Cells[5].Value.ToString());
                    DataTable tb = kh.Select_DK(dgvKhachHang.SelectedRows[0].Cells[1].Value.ToString(), dgvKhachHang.SelectedRows[0].Cells[2].Value.ToString(), dgvKhachHang.SelectedRows[0].Cells[4].Value.ToString(), dgvKhachHang.SelectedRows[0].Cells[5].Value.ToString());

                    if (tb.Rows.Count > 0)
                    {
                        HT();
                        thongBao.KiemTraTruocKhiThem();
                    }
                    else
                    {
                        int count = 0;
                        khachHang.DonviB = xl.ChuanHoa(dgvKhachHang.CurrentRow.Cells[1].Value.ToString());
                        khachHang.DaidienB = xl.ChuanHoa(dgvKhachHang.CurrentRow.Cells[2].Value.ToString());
                        khachHang.ChucvuB = xl.ChuanHoa(dgvKhachHang.CurrentRow.Cells[3].Value.ToString());
                        khachHang.DiachiB = xl.ChuanHoa(dgvKhachHang.CurrentRow.Cells[4].Value.ToString());
                        khachHang.DienthoaiB = dgvKhachHang.CurrentRow.Cells[5].Value.ToString();

                        //Int64 sdt = Int64.Parse(dgvKhachHang.CurrentRow.Cells[5].Value.ToString());
                        if (CheckSDT(dgvKhachHang.CurrentRow.Cells[5].Value.ToString()) == true)
                        {
                            count += kh.Update(khachHang, "maKH='" + dgvKhachHang.CurrentRow.Cells[0].Value.ToString() + "'");
                            if (count > 0)
                            {
                                HT();
                            }
                            else
                            {
                                HT();
                                thongBao.LoiSua();
                            }
                        }
                        else
                        {
                            HT();
                        }
                    }
                }
                else
                {
                    thongBao.KiemTraSo();
                }
        }
    }


        public bool CheckSDT(string sdt)
        {
            if (sdt.Length > 0 && sdt.Length <13 && xl.IsNumber(sdt) == true && Int64.Parse(sdt) >0)
            {   
                return true;
            }
            else
            {
                HT();
                thongBao.KiemTraSo();
                return false;  
            }
        }


        private void btnLoadLai_Click(object sender, EventArgs e)
        {
            HT();
        }
        public void STT()
        {
            int stt = 0;
            for (int i = 0; i < dgvKhachHang.Rows.Count - 1; i++)
            {
                stt = stt + 1;
                dgvKhachHang.Rows[i].Cells[1].Value = stt;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvKhachHang.SelectedRows.Count > 0)
            {
                DataTable tbXoa = kh.selectDKXoa(dgvKhachHang.CurrentRow.Cells[0].Value.ToString());
                if (tbXoa.Rows.Count > 0)
                {
                    thongBao.KTNhom(dgvKhachHang.CurrentRow.Cells[1].Value.ToString());
                }
                else
                {
                    int count = 0;
                    foreach (DataGridViewRow dr in dgvKhachHang.SelectedRows)
                    {
                        count += kh.Delete("maKH='" + dr.Cells[0].Value.ToString() + "'");
                    }
                    if (count > 0)
                    {
                        HT();
                        thongBao.Kq_Xoa(count);
                    }
                    else
                    {
                        thongBao.LoiXoa();
                    }
                }
            }
        }
        
        //kiem tra quyen(Mr.Tuyen)
        public void CheckQuyen()
        {
            BUser user = new BUser();
            DataTable sua = user.Select_Quyen("name='" + ten + "'", "suaKH='X'");
            DataTable xoa = user.Select_Quyen("name='" + ten + "'", "xoaKH='X'");
            if (sua.Rows.Count > 0)
            {
                dgvKhachHang.ReadOnly = false;
            }
            else
            {
                dgvKhachHang.ReadOnly = true;
            }
            if (xoa.Rows.Count > 0)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text != "")
            {
                if (rdMaKH.Checked == true)
                {
                    TK("maKH='" + xl.ChuanHoa(txtTimKiem.Text) + "'");
                }
                else
                {
                    if (rdTenKH.Checked == true)
                    {
                        TK("donviB LIKE N'%" + @xl.ChuanHoa(txtTimKiem.Text) + "%'");
                    }
                    else
                    {
                        thongBao.TimKiem();
                    }
                }
            }
        }
        public void TK(string dk)
        {
            ArrayList khachHang = kh.Select_TK(dk);
            DataTable tbKH = new DataTable();

            //tbKH.Columns.Add("STT", typeof(String));
            tbKH.Columns.Add("Mã KH", typeof(string));
            tbKH.Columns.Add("Tên Công ty(Đơn vị)", typeof(String));
            tbKH.Columns.Add("Đại diện", typeof(String));
            tbKH.Columns.Add("Chức vụ", typeof(String));
            tbKH.Columns.Add("Địa chỉ", typeof(String));
            tbKH.Columns.Add("Số DT", typeof(String));
            if (khachHang.Count > 0)
            {
                foreach (KHACHHANG tx in khachHang)
                {
                    DataRow r = tbKH.NewRow();
                    r["Mã KH"] = tx.MaKH;
                    r["Tên Công ty(Đơn vị)"] = tx.DonviB;
                    r["Đại diện"] = tx.DaidienB;
                    r["Chức vụ"] = tx.ChucvuB;
                    r["Địa chỉ"] = tx.DiachiB;
                    r["Số DT"] = tx.DienthoaiB;
                    tbKH.Rows.Add(r);
                }
                dgvKhachHang.DataSource = tbKH;
                //dgvKhachHang.Columns[0].Visible = false;
                dgvKhachHang.RowHeadersVisible = false;
                dgvKhachHang.Columns[0].ReadOnly = true;
                dgvKhachHang.Columns[0].Width = 100;
                dgvKhachHang.Columns[1].Width = 388;
                dgvKhachHang.Columns[2].Width = 250;
                dgvKhachHang.Columns[3].Width = 120;
                dgvKhachHang.Columns[4].Width = 340;
                dgvKhachHang.Columns[5].Width = 150;
            }
            else
            {
                thongBao.DKTimKiem();
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            btnTimKiem.Enabled = true;
        }
    }
}
